<?php
declare (strict_types = 1);

namespace app\admin\model\admin;

use app\admin\service\MenuService;
use app\admin\service\UserService;
use think\facade\Cache;
use think\facade\Session;
use think\facade\Cookie;

/**
 * 管理员表模型
 */
class AdminUser extends BaseModel
{
   
    /**
     * 用户登录验证
     */
    public function login($username = '', $password = '', $isRemember = false)
    {
        //验证账户密码
        $admin = self::where('username',$username)->find();
        if (!$admin) {
            $this->error = '用户不存在';
            return false;
        }
        if( $admin->status == self::DISABLED_STATE ){
            $this->error = '用户已经被禁用';
            return false;
        }
        if( $admin->password != substr(md5(trim($password)),8,16) ){
            $this->error = '输入的密码错误';
            return false;
        }
        //是否记住密码
        $hash = simple_encrypt($admin->id.'###'.$admin->password);
        if ($isRemember) {
            Cookie::set('hash', $hash,7 * 86400);
        } else {
            Cookie::set('hash', $hash,null);
        }
        // 用户信息存入会话
        Session::set('admin', [
            'id' => $admin->id,
            'role_ids' => $admin->role_ids,
            'username' => $admin->username,
            'nickname' => $admin->nickname,
            'password' => $admin->password,
            'headimgurl' => $admin->headimgurl
        ]);
        Cache::set("admin:{$admin->id}:menu",MenuService::getListByUid($admin->id));
        // 触发登录成功事件,记录管理员日志
        //event('AdminLogin');
        return true;
    }

    /**
     * 判断是否登录
     * @return bool|array
     */
    public function isLogin()
    {
        return Session::get('admin');
    }
    
    /**
     * 判断是否登录
     * @return bool|array
     */
    public function isLogin2()
    {
        $admin = Session::get('admin');
        $hash = Cookie::get('hash');

        if (!$admin && !$hash) return false;
        //判断Session是否存在
        if (!$admin) {
            $hash = explode('###', simple_decrypt($hash?$hash:'-'));
            if (!isset($hash[1]) && !isset($hash[2])) return false;
            $info = self::field(true)->where(['id'=>$hash[0],'password'=>$hash[1],'status'=>1])->find();
            if(!$info) return false;
            // 缓存登录信息
            $data = [
                'id' => $info->id,
                'username' => $info->username,
                'nickname' => $info->nickname,
                'password' => $info->password,
                'menu' => $this->permissions($info->id),
                'campus' => $info->campus_id
            ];
            Session::set('admin', $data);
            return true;
         }
         //判断Cookie是否存在
         if(!$hash){
            $info = self::field(true)->where(['id'=>$admin['id'],'password'=>$admin['password'],'status'=>1])->find();
            if($info){
                $hash = simple_encrypt($info->id.'###'.$info->password);
                Cookie::set('hash', $hash,null);
               return true;
            }
         }
        return true;
    }

     /**
     * 退出登陆
     * @return bool
     */
    public function logout()
    {
        Session::delete('admin');
        Cookie::delete('hash');
        return true;
    }

    /**
     * 定义获取器 获取用户所在组
     */
    public function getRoleIdsAttr($value,$data)
   {
        $userModel = self::with('roles')->find($data['id'])->toArray();
        if( $userModel != null ){
            return array_column($userModel['roles'],'id');
        }
   }
    
    /**
     * 管理拥有的角色
     */
    public function roles()
    {
        return $this->belongsToMany(AdminRole::class, AdminUserRole::class, 'role_id','admin_id');
    }

    public function hasRoles()
    {
        return $this->hasMany(AdminUserRole::class,'admin_id','id');
    }


}
